#include "SkyBox.h"
#include "ModelLoader.h"

SkyBox::SkyBox(string dirPath, string left, string back, string right, string front, string top, string bottom)
{
	string filePath;
	filePath = dirPath + left;
	imageId[SKY_LEFT] = ModelLoader::get()->loadTexture(filePath.c_str());

	filePath = dirPath + back;
	imageId[SKY_BACK] = ModelLoader::get()->loadTexture(filePath.c_str());

	filePath = dirPath + right;
	imageId[SKY_RIGHT] = ModelLoader::get()->loadTexture(filePath.c_str());

	filePath = dirPath + front;
	imageId[SKY_FRONT] = ModelLoader::get()->loadTexture(filePath.c_str());

	filePath = dirPath + top;
	imageId[SKY_TOP] = ModelLoader::get()->loadTexture(filePath.c_str());

	filePath = dirPath + bottom;
	imageId[SKY_BOTTOM] = ModelLoader::get()->loadTexture(filePath.c_str());
}

SkyBox::~SkyBox()
{
	glDeleteTextures(6, imageId);
}

void SkyBox::draw(float factor)
{
	glDisable(GL_DEPTH_TEST);
	glEnable(GL_TEXTURE_2D);
	glBindTexture(GL_TEXTURE_2D, imageId[SKY_FRONT]);
	glBegin(GL_QUADS);
		// Front Face
		glTexCoord2f(0.0f, 0.0f); glVertex3f(-1.0f * factor, -1.0f * factor,  1.0f * factor);  // Bottom Left Of The Texture and Quad
		glTexCoord2f(1.0f, 0.0f); glVertex3f(1.0f * factor, -1.0f * factor,  1.0f * factor);  // Bottom Right Of The Texture and Quad
		glTexCoord2f(1.0f, 1.0f); glVertex3f(1.0f * factor,  1.0f * factor,  1.0f * factor);  // Top Right Of The Texture and Quad
		glTexCoord2f(0.0f, 1.0f); glVertex3f(-1.0f * factor,  1.0f * factor,  1.0f * factor);  // Top Left Of The Texture and Quad
	glEnd();
	glBindTexture(GL_TEXTURE_2D, imageId[SKY_BACK]);
	glBegin(GL_QUADS);
		// Back Face
		glTexCoord2f(1.0f, 0.0f); glVertex3f(-1.0f * factor, -1.0f * factor, -1.0f * factor);  // Bottom Right Of The Texture and Quad
		glTexCoord2f(1.0f, 1.0f); glVertex3f(-1.0f * factor,  1.0f * factor, -1.0f * factor);  // Top Right Of The Texture and Quad
		glTexCoord2f(0.0f, 1.0f); glVertex3f(1.0f * factor,  1.0f * factor, -1.0f * factor);  // Top Left Of The Texture and Quad
		glTexCoord2f(0.0f, 0.0f); glVertex3f(1.0f * factor, -1.0f * factor, -1.0f * factor);  // Bottom Left Of The Texture and Quad
	glEnd();
	glBindTexture(GL_TEXTURE_2D, imageId[SKY_TOP]);
	glBegin(GL_QUADS);
		// Top Face
		glTexCoord2f(0.0f, 1.0f); glVertex3f(-1.0f * factor,  1.0f * factor, -1.0f * factor);  // Top Left Of The Texture and Quad
		glTexCoord2f(0.0f, 0.0f); glVertex3f(-1.0f * factor,  1.0f * factor,  1.0f * factor);  // Bottom Left Of The Texture and Quad
		glTexCoord2f(1.0f, 0.0f); glVertex3f(1.0f * factor,  1.0f * factor,  1.0f * factor);  // Bottom Right Of The Texture and Quad
		glTexCoord2f(1.0f, 1.0f); glVertex3f(1.0f * factor,  1.0f * factor, -1.0f * factor);  // Top Right Of The Texture and Quad
	glEnd();
	glBindTexture(GL_TEXTURE_2D, imageId[SKY_BOTTOM]);
	glBegin(GL_QUADS);
		// Bottom face
		glTexCoord2f(1.0f, 1.0f); glVertex3f(-1.0f * factor, -1.0f * factor, -1.0f * factor);  // Top Right Of The Texture and Quad
		glTexCoord2f(0.0f, 1.0f); glVertex3f(1.0f * factor, -1.0f * factor, -1.0f * factor);  // Top Left Of The Texture and Quad
		glTexCoord2f(0.0f, 0.0f); glVertex3f(1.0f * factor, -1.0f * factor,  1.0f * factor);  // Bottom Left Of The Texture and Quad
		glTexCoord2f(1.0f, 0.0f); glVertex3f(-1.0f * factor, -1.0f * factor,  1.0f * factor);  // Bottom Right Of The Texture and Quad
	glEnd();
	glBindTexture(GL_TEXTURE_2D, imageId[SKY_RIGHT]);
	glBegin(GL_QUADS);		
		// Right face
		glTexCoord2f(1.0f, 0.0f); glVertex3f(1.0f * factor, -1.0f * factor, -1.0f * factor);  // Bottom Right Of The Texture and Quad
		glTexCoord2f(1.0f, 1.0f); glVertex3f(1.0f * factor,  1.0f * factor, -1.0f * factor);  // Top Right Of The Texture and Quad
		glTexCoord2f(0.0f, 1.0f); glVertex3f(1.0f * factor,  1.0f * factor,  1.0f * factor);  // Top Left Of The Texture and Quad
		glTexCoord2f(0.0f, 0.0f); glVertex3f(1.0f * factor, -1.0f * factor,  1.0f * factor);  // Bottom Left Of The Texture and Quad
	glEnd();
	glBindTexture(GL_TEXTURE_2D, imageId[SKY_LEFT]);
	glBegin(GL_QUADS);
		// Left Face
		glTexCoord2f(0.0f, 0.0f); glVertex3f(-1.0f * factor, -1.0f * factor, -1.0f * factor);  // Bottom Left Of The Texture and Quad
		glTexCoord2f(1.0f, 0.0f); glVertex3f(-1.0f * factor, -1.0f * factor,  1.0f * factor);  // Bottom Right Of The Texture and Quad
		glTexCoord2f(1.0f, 1.0f); glVertex3f(-1.0f * factor,  1.0f * factor,  1.0f * factor);  // Top Right Of The Texture and Quad
		glTexCoord2f(0.0f, 1.0f); glVertex3f(-1.0f * factor,  1.0f * factor, -1.0f * factor);  // Top Left Of The Texture and Quad
	glEnd();
	glDisable(GL_TEXTURE_2D);
	glEnable(GL_DEPTH_TEST);
}